package com.example.college;



import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;



import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Grades extends Activity {

	    // Progress Dialog
		private ProgressDialog pDialog;

		JSONParser jsonParser = new JSONParser();

	
	EditText inputsname;
	EditText inputcoursename;
	EditText inputfinal;
	
	// url to add grades
		private static String url_add_grades = "http://10.0.2.2/android_connect/create_product.php";

		// JSON Node names
		private static final String TAG_SUCCESS = "success";

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grades);
		
		//Edit Text
		inputsname=(EditText) findViewById(R.id.Student_NAME);
		inputcoursename=(EditText) findViewById(R.id.COURSE_NAME);
		inputfinal=(EditText) findViewById(R.id.finalgrade);
		
		//create button
		
		Button btnaddgrade=(Button) findViewById(R.id.add_grade);
		
		//button click event
		btnaddgrade.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// adding grades in background thread
				new AddGrade().execute();
				
			}
		});
		
		
	}
	
	
	/**
	 * Background Async Task to Add Grades
	 * */
	
	class AddGrade extends AsyncTask<String, String, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog=new ProgressDialog(Grades.this);
			pDialog.setMessage("Adding Grades..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}
		
		/**
		 * Adding Grades
		 * */
		
		@Override
		protected String doInBackground(String... args) {
			String id=inputsname.getText().toString();
			String coursename=inputcoursename.getText().toString();
			String grade=inputfinal.getText().toString();
			
			// Building Parameters
			 List<NameValuePair> params = new ArrayList<NameValuePair>();
			 params.add(new BasicNameValuePair("ID", id));
			 params.add(new BasicNameValuePair("Midterm", coursename));
			 params.add(new BasicNameValuePair("Grade", grade));
			 
			// getting JSON Object
				// Note that adding grades url accepts POST method
				JSONObject json = jsonParser.makeHttpRequest(url_add_grades,
						"POST", params);
				
				// check log cat for response
				Log.d("Add Response", json.toString());
				
				// check for success tag
				try {
					int success = json.getInt(TAG_SUCCESS);

					if (success == 1) {
						// successfully grades added
						Toast toast=Toast.makeText(getApplicationContext(), "Addition success", Toast.LENGTH_LONG);
						toast.show();
						
						// closing this screen
						finish();
					} else {
						// failed to add grades
						Toast toast=Toast.makeText(getApplicationContext(), "Addition Failed", Toast.LENGTH_LONG);
						toast.show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			
			return null;
		}
		
		
		
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}
		
		
	}
	

	

}
